package com.example.fund.candle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.fund.info.FundInfo;
import com.example.fund.util.DBManager;
import com.example.fund.util.FundUtil;
import com.example.fund.util.FundUtil1;

public class CandleImpl implements ICandle {

	@Override
	public FundInfo getRefFundInfo() {
		FundInfo info = FundUtil.getFundNetValueFromLocalhost("中证500","0000905");
		return info;
	}
	
	@Override
	public FundInfo getRefFundInfo1() {
		FundInfo info = FundUtil.getFundNetValueFromLocalhost("上证","0000001");
		return info;
	}
	
	public Set<String> getRefFundInfo1Range()
	{
		Set<String> set = new HashSet<String>();
		
		SQLiteDatabase db = DBManager.getDB();

		String sql = "select number from indexset where refNumber='A'";
		Cursor cs = db.rawQuery(sql.toString(), null);
		
		while (cs.moveToNext())
		{
            String number = cs.getString(cs.getColumnIndex("number"));
            
            set.add(number);
		}
		cs.close();
		
		return set;
	}

	@Override
	public List<FundInfo> getFundInfoList() {
		List<FundInfo> list = new ArrayList<FundInfo>();
        List<Map<String,Object>> indexList = FundUtil1.getIndexSet();
		for(int i=0;i<indexList.size();i++)
    	{
			Map<String, Object> map = indexList.get(i);
			String name = null;
			String number = null;
			int enable = 0;
			if(map.get("name")!=null)
			{
				name = map.get("name").toString();
			}
			if(map.get("number")!=null)
			{
				number = map.get("number").toString();
			}
			if(map.get("enable")!=null)
			{
				enable = Integer.valueOf(map.get("enable").toString());
			}
			
			if(name==null || number==null || number.trim().length()==0 || enable==0)
			{
				continue;
			}
			
    		FundInfo info = FundUtil.getFundNetValueFromLocalhost(name,number);
    		list.add(info);
    	}
		return list;
	}

	@Override
	public List<CandleInfo> getCandleInfoList() {
		List<CandleInfo> list = new LinkedList<CandleInfo>();
		
		FundInfo refInfo = getRefFundInfo();
		if(refInfo==null)
		{
			return list;
		}
		
		FundInfo refInfo1 = getRefFundInfo1();
		if(refInfo1==null)
		{
			return list;
		}
		
		List<FundInfo> infoList = getFundInfoList();
		if(infoList==null)
		{
			return list;
		}
		
		List<String> dateList = FundUtil.getSHIndexDateList();
		if(dateList==null)
		{
			return list;
		}
		
		
		Map<String,String> startDateMap = new HashMap<String,String>();
		List<Map<String, Object>> idxSetList = FundUtil1.getIndexSet();
		for(int i=0;i<idxSetList.size();i++)
		{
			Map<String, Object> inxSet = idxSetList.get(i);
			Object number = inxSet.get("number");
			Object defDate = inxSet.get("defDate");
			if(number!=null && number.toString().trim().length()>0
					&& defDate!=null && defDate.toString().trim().length()>0)
			{
				startDateMap.put(number.toString().trim(), defDate.toString().trim());
			}
		}
		int startDefIdx = dateList.size()-1;
		for(int i=startDefIdx;i>=0;i--)
		{
			if("2005-07-11".compareTo(dateList.get(i))<=0)
			{
				startDefIdx = i;
				break;
			}
		}
		Map<String,Integer> startIdxMap = new HashMap<String,Integer>();
		for(String number:startDateMap.keySet())
		{
			int startIdx = dateList.size()-1;
			String defDate = startDateMap.get(number);
			for(int i=startIdx;i>=0;i--)
			{
				if(defDate.compareTo(dateList.get(i))<=0)
				{
					startIdx = i;
					startIdxMap.put(number, startIdx);
					break;
				}
			}
		}
		
		Set<String> refFundInfo1Range = getRefFundInfo1Range();
		
		List<FundInfo> diffInfoList = new ArrayList<FundInfo>();
		for(int i=0,l=infoList.size();i<l;i++)
		{
			FundInfo info = infoList.get(i);
			String number = info.getNumber();
			if(refInfo.getNumber().equals(number)
					||refInfo1.getNumber().equals(number))
			{
				continue;
			}
			FundInfo refInfo2 = refInfo;
			if(refFundInfo1Range.contains(number))
			{
				refInfo2 = refInfo1;
			}
			FundInfo cloneInfo = new FundInfo();
			cloneInfo.setName(info.getName());
			cloneInfo.setNumber(info.getNumber());
			Map<String,String> actValues = cloneInfo.getActualValues();
			Map<String,String> actValues0 = refInfo2.getActualValues();
			Map<String,String> actValues1 = info.getActualValues();
			Integer startIdx = startIdxMap.get(number);
			if(startIdx==null)
			{
				startIdx = startDefIdx;
			}
			for(int j=startIdx;j>=0;j--)
			{
				String dateStr = dateList.get(j);
				String actValue0 = actValues0.get(dateStr);
				String actValue1 = actValues1.get(dateStr);
				if(actValue0!=null && actValue1!=null && Double.valueOf(actValue0)!=0)
				{
					double actValue = Double.valueOf(actValue1)/Double.valueOf(actValue0);
					actValues.put(dateStr, String.valueOf(actValue));
				}
			}
			diffInfoList.add(cloneInfo);
		}
		
		for(int i=0,l=diffInfoList.size();i<l;i++)
		{
			FundInfo info = diffInfoList.get(i);
			String name = info.getName();
			String number = info.getNumber();
			Map<String, String> actValues = info.getActualValues();
			boolean isInit = false;
			double maxPrice = -1;
			double minPrice = -1;
			double closePrice = -1;
			double closePosition = -1;
			Integer startIdx = startIdxMap.get(number);
			if(startIdx==null)
			{
				startIdx = startDefIdx;
			}
			for(int j=startIdx;j>=0;j--)
			{
				String dateStr = dateList.get(j);
				String actValue = actValues.get(dateStr);
				if(actValue==null)
				{
					continue;
				}
				double value = Double.valueOf(actValue);
				if(!isInit)
				{
					maxPrice = value;
					minPrice = value;
					isInit = true;
				}
				if(value>maxPrice)
				{
					maxPrice = value;
				}
				if(value<minPrice)
				{
					minPrice = value;
				}
				closePrice = value;
			}
			if(!isInit)
			{
				continue;
			}
			
			if(maxPrice>minPrice)
			{
				closePosition = (closePrice-minPrice)/(maxPrice-minPrice);
			}
			CandleInfo candle = new CandleInfo(number,name);
			candle.setMaxPrice((double)Math.round(maxPrice*1000000)/1000000);
			candle.setMinPrice((double)Math.round(minPrice*1000000)/1000000);
			candle.setClosePrice((double)Math.round(closePrice*1000000)/1000000);
			candle.setClosePosition((double)Math.round(closePosition*1000000)/1000000);
			
			list.add(candle);
		}
		
		Collections.sort(list, new Comparator<CandleInfo>() {

			@Override
			public int compare(CandleInfo info, CandleInfo info1) 
			{
				if(info.getClosePosition()>info1.getClosePosition())
				{
					return 1;
				}else if(info.getClosePosition()<info1.getClosePosition())
				{
					return -1;
				}else
				{
					return 0;
				}
			}
		});
		
		return list;
	}

}
